package com.shundr.shipper.cargo;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.cargo.http.CargoRequest;
import com.shundr.shipper.cargo.model.CargoTemplateInfo;
import com.shundr.shipper.common.util.DateUtil;
import com.shundr.shipper.common.util.DialogUtil;
import com.shundr.shipper.common.util.JsonUtil;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.DialogDisplay.OnDisplayListener;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.util.StringUtil;

/**
 * 货源详情
 * CargoDetailsActivity
 * 
 * @author larry 2015-5-20 下午2:52:22 
 * @version 1.0.0
 *
 */
public class CargoTemplateDetailActivity extends BaseActivity {
	/**
	 * 始发地/目的地
	 */
	private TextView tvSrcPlace, tvDestPlace;
	/**
	 * 货物信息
	 */
	private TextView tvCargoName;
	/**
	 * 车辆需求
	 */
	private TextView tvTruckRequire;
	/**
	 * 信息来源
	 */
	private TextView tvInfoFrom;
	/**
	 * 货物价格
	 */
	private TextView tvCargoPrice;
	/**
	 * 联系方式
	 */
	private TextView tvContact;
	/**
	 * 发布时间
	 */
	private TextView tvInTime;
	
	private TextView tvExpiry;
	
	private TextView tvRemark;
	private TextView tvStatus;

	private CargoTemplateInfo mInfo;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case CargoRequest.DELETE_CARGO_TEMPLATE_SUCCESSED:
				ProgressDialogHelper.dismiss();
				ToastHelper.toastShort(mContext, "删除成功");
				new Timer().schedule(new TimerTask() {
					
					@Override
					public void run() {
						setResult(888);
						finish();
					}
				}, 600);
				break;
			case LHttpRequest.REQUEST_FAILED:
				ProgressDialogHelper.dismiss();
				ToastHelper.toastShort(mContext, (String)msg.obj);
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cargo_template_detail);
		tvSrcPlace = (TextView) findViewById(R.id.cargo_details_src);
		tvDestPlace = (TextView) findViewById(R.id.cargo_details_dst);
		tvCargoName = (TextView) findViewById(R.id.cargo_details_cargoinfo);
		tvTruckRequire = (TextView) findViewById(R.id.cargo_details_truck_require);
		tvInfoFrom = (TextView) findViewById(R.id.cargo_details_information_from);
		tvCargoPrice = (TextView) findViewById(R.id.cargo_details_price);
		tvContact = (TextView) findViewById(R.id.cargo_details_contact);
		tvInTime = (TextView) findViewById(R.id.cargo_details_in_time);
		tvExpiry = (TextView) findViewById(R.id.cargo_details_expiry_time);
		tvRemark = (TextView) findViewById(R.id.tv_remark);
		tvStatus = (TextView) findViewById(R.id.tv_status);
		
		String json = getIntent().getStringExtra("json");
		mInfo = JsonUtil.fromJson(json, CargoTemplateInfo.class);
		String title = getIntent().getStringExtra("title");
		if(!StringUtil.isNull(title)){
			setTitle(title);
		}
		showCargoDetails(mInfo);
	}

	private void showCargoDetails(CargoTemplateInfo info) {
		try {
			if (!StringUtil.isNull(info.getCargoSrcPlace())) {
				tvSrcPlace.setText(info.getCargoSrcPlace());
			}
			if (!StringUtil.isNull(info.getCargoDstPlace())) {
				tvDestPlace.setText(info.getCargoDstPlace());
			}
			String cargoCategory = "";
			String cargoAmount = "";
			if (!StringUtil.isNull(info.getCargoCategory())) {
				cargoCategory = info.getCargoCategory() + "  ";
			}
			if (!StringUtil.isNull(info.getCargoAmount())) {
				cargoAmount = info.getCargoAmount() + info.getCargoUnitName();
			}
			tvCargoName.setText(cargoCategory + cargoAmount);
			String truckType = "";
			String truckLength = "";
			String truckNum = "";
			if (!StringUtil.isNull(info.getCargoTruckType())) {
				truckType = info.getCargoTruckType() + "  ";
			}
			if (!StringUtil.isNull(info.getCargoTruckLength() + "")) {
				if(StringUtil.isEqual(info.getCargoTruckLength(), "23")){
					truckLength = info.getCargoTruckLength() + "米以上" + "  ";
				}else{
					truckLength = info.getCargoTruckLength() + "米" + "  ";
				}
			}
			if (!StringUtil.isNull(info.getCargoTruckNum())) {
				truckNum = info.getCargoTruckNum() + "辆";
			}
			String require = truckType + truckLength + truckNum;
			if(StringUtil.isNull(require)){
				tvTruckRequire.setText("不限");
			}else{
				tvTruckRequire.setText(require);
			}
			if(!StringUtil.isNull(info.getCargoContactPerson())){
				tvInfoFrom.setText(info.getCargoContactPerson());
			}else if (!StringUtil.isNull(info.getCargoCompanyName())) {
				tvInfoFrom.setText(info.getCargoCompanyName());
			}else{
				tvInfoFrom.setText("未知");
			}
			if (!StringUtil.isNull(info.getCargoUnitPrice())) {
				tvCargoPrice.setText(info.getCargoUnitPrice() + " 元/ " + info.getCargoUnitName());
			}else{
				tvCargoPrice.setText("面谈");
			}
			if (!StringUtil.isNull(info.getCargoCompanyPhone1())) {
				String phone=info.getCargoCompanyPhone1();
				tvContact.setText(phone);
			}
			if (!StringUtil.isNull(info.getCargoInsertTime())) {
				tvInTime.setText(info.getCargoInsertTime());
			}
			if (!StringUtil.isNull(info.getCargoExpiryTime())) {
				tvExpiry.setText(DateUtil.getDate_1(info.getCargoExpiryTime()));
			}else{
				tvExpiry.setText("不限");
			}
			if (!StringUtil.isNull(info.getCargoRemarks())) {
				tvRemark.setText(info.getCargoRemarks());
			}else{
				tvRemark.setText("无");
			}
			if (StringUtil.isEqual(info.getCargoStatus() + "", "1")) {
				tvStatus.setText("待成交");
			} else {
				tvStatus.setText("已下架");
				tvStatus.setTextColor(getResources().getColor(R.color.btn_orange_light));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_confirm, menu);
		menu.getItem(0).setTitle("删除");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_confirm:
			DialogUtil.noticeAlertDialog(mContext, "删除", "是否确定删除此车辆？",
					new OnDisplayListener() {

						@Override
						public void confirm() {
							ProgressDialogHelper.show(mContext, "正在删除中...");
							new CargoRequest(mContext,mHandler).deleteCargoTemplate(mInfo.getMd5Id());
						}

						@Override
						public void cancel() {

						}
					});
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onDestroy() {
		mHandler.removeCallbacksAndMessages(null);
		super.onDestroy();
	}

}
